<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>班级信息管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../static/layui/css/layui.css" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<div class="layui-row" id="editCls" style="display:none;">
    <div class="layui-col-md10">
        <form class="layui-form layui-from-pane" action="" style="margin-top:20px">
            <div class="layui-form-item">
                <label class="layui-form-label">班级名</label>
                <div class="layui-input-block">
                    <input type="text" name="editName" id="editName" required lay-verify="required" autocomplete="off"
                           placeholder="请输入班级名" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">班级编号</label>
                <div class="layui-input-block">
                    <input type="text" name="editNum" id="editNum" required lay-verify="required" autocomplete="off"
                           placeholder="请输入班级编号" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">学院</label>
                <div class="layui-input-block">
                    <select name="departmentId" lay-filter="department" id="departmentId">
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">班主任</label>
                <div class="layui-input-block">
                    <select name="banZhuRenId" lay-filter="username" id="banZhuRenId"></select>
                </div>
            </div>

        </form>
    </div>
</div>

<div class="layui-btn-group actBar" style="float: left;">
    <shiro:hasPermission name="cls:add">
        <button class="layui-btn layui-btn-sm" data-type="add"><i class="layui-icon"></i>添加</button>
    </shiro:hasPermission>
</div>

<shiro:hasPermission name="cls:findAll">
    <div class="actBar" style="margin-left: 400px;">
        搜索：
        <div class="layui-inline">
            <input class="layui-input" name="id" id="fuzzySearch" autocomplete="off">
        </div>
        <button class="layui-btn" data-type="fuzzySearch">搜索</button>
    </div>
</shiro:hasPermission>

<table class="layui-table" lay-data="{ url:'/cls/findAllInfo', page:true, id:'clsList'}" lay-filter="demo">
    <thead>
    <tr>
        <th lay-data="{type: 'checkbox', align:'center', width: 40, fixed: 'left'}"></th>
        <th lay-data="{field:'id', align:'center', width:100, sort: true, fixed: true}">ID</th>
        <th lay-data="{field:'num', align:'center', width:150, sort: true}">班级号</th>
        <th lay-data="{field:'name', align:'center', width:200}">班级名</th>
        <th lay-data="{field:'banZhuRenName', align:'center', width:150}">班主任</th>
        <th lay-data="{field:'departmentName', align:'center', width:150}">学院</th>


        <th lay-data="{fixed: 'right', align:'center', width:178,  toolbar: '#barDemo'}"></th>
    </tr>
    </thead>
</table>


<script type="text/html" id="barDemo">
    <shiro:hasPermission name="cls:update">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    </shiro:hasPermission>
    <shiro:hasPermission name="cls:del">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </shiro:hasPermission>
</script>


<script src="../static/layui/layui.js" charset="utf-8"></script>
<script src="../static/js/jquery.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->
<script>
    var _form;
    //获取选中ID
    layui.use(['form', 'table'], function () {
        var form = layui.form
            , table = layui.table;
        _form=form;
        //监听表格复选框选择
        table.on('checkbox(demo)', function (obj) {
            console.log(obj)
        });
        //监听工具条
        table.on('tool(demo)', function (obj) {
                var data = obj.data;
                if (obj.event === 'detail') {
                    layer.msg('ID：' + data.id + ' 的查看操作');
                } else if (obj.event === 'del') {
                    layer.confirm('确认删除班级吗？', function (index) {
                        $.ajax({
                            url: '/cls/del?id=' + data.id,
                            type: "get",
                            dataType: "json",
                            success: function (res) {
                                if (res.code === 200) {
                                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                    layer.close(index);
                                    console.log(index);
                                    layer.msg("删除成功", {
                                        icon: 1
                                    });
                                } else {
                                    layer.msg("删除失败", {
                                        icon: 5
                                    });
                                }
                            }
                        });
                    });
                } else if (obj.event === 'edit') {
                    layer.open({
                        //layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
                        type: 1,
                        title: "修改班级信息",
                        content: $("#editCls")//引用的弹出层的页面层的方式加载修改界面表单
                        , btn: ['确定', '取消'],
                        success: function (layero, index) {
                            $("#editName").val(data.name);
                            $("#editNum").val(data.num);
                            getDept();
                        },
                        btn1: function (index, layero) {
                            $.ajax({
                                url: '/cls/update',
                                type: "post",
                                dataType: "json",
                                data: {
                                    id: data.id,
                                    name: $("#editName").val(),    //班级名
                                    num: $("#editNum").val(),        //班级编号
                                    departmentId: $("#departmentId").val(),
                                    banZhuRenId: $("#banZhuRenId").val()
                                },
                                success: function (res) {
                                    if (res.code === 200) {
                                        obj.update({
                                            name: $("#editName").val(),    //班级名
                                            num: $("#editNum").val(),        //班级编号
                                            departmentName: $("#departmentId").find("option:selected").text(),
                                            banZhuRenName: $("#banZhuRenId").find("option:selected").text()
                                        });
                                        layer.close(index);
                                        console.log(index);
                                        layer.msg("更新成功", {
                                            icon: 1
                                        });
                                    } else {
                                        layer.msg("更新失败", {
                                            icon: 5
                                        });
                                    }
                                }
                            });
                        }
                        , end: function () {
                            $("#editName").val('');
                            $("#editNum").val('');
                            $("select").empty();
                            $("#editCls").attr("style", "display:none;");

                        }
                    });
                    //动态向表传递赋值可以参看文章进行修改界面的更新前数据的显示，当然也是异步请求的要数据的修改数据的获取
                    // setFormValue(obj, data);
                }
            },
            form.on('select(department)', function (data) {
                $.getJSON("/user/getUsers?deptId=" + data.value, function (data) {
                    var optionstring = "";
                    $.each(data.data, function (i, item) {
                        optionstring += "<option value=\"" + item.id + "\" >" + item.name + "</option>";
                    });
                    $("#banZhuRenId").html('<option value=""></option>' + optionstring);
                    form.render('select'); //这个很重要
                });
            })
        );

        var $ = layui.$, active = {
            add: function () {  //添加班级
                layer.open({
                    //layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
                    type: 1,
                    title: "添加班级信息",
                    content: $("#editCls")//引用的弹出层的页面层的方式加载修改界面表单
                    , btn: ['确定', '取消'],
                    success: function () {
                        getDept();
                    },
                    btn1: function (index, layero) {
                        $.ajax({
                            url: '/cls/add',
                            type: "post",
                            dataType: "json",
                            data: {
                                name: $("#editName").val(),    //班级名
                                num: $("#editNum").val()   ,     //班级编号
                                departmentId: $("#departmentId").val(),
                                banZhuRenId: $("#banZhuRenId").val()
                            },
                            success: function (res) {
                                if (res.code === 200) {
                                    layer.close(index);
                                    console.log(index);
                                    layer.msg("添加成功", {
                                        icon: 1
                                    }, function () {
                                        window.location.reload();
                                    });
                                } else {
                                    layer.msg("添加失败", {
                                        icon: 5
                                    });
                                }
                            }
                        });
                    },
                    btn2: function (index, layero) {
                    }
                    , end: function () {
                        $("#editName").val('');
                        $("#editNum").val('');
                        $("select").empty();
                        $("#editCls").attr("style", "display:none;");

                    }
                });
            },

            fuzzySearch: function () {
                var fuzzySearch = $('#fuzzySearch');

                //执行重载
                table.reload('clsList', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        key: fuzzySearch.val()
                    }
                });
            }
        };

        $('.actBar .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });

    function getDept() {
        $.getJSON("/department/findAll", function (data) {
            var optionstring = "";
            $.each(data.data, function (i, item) {
                optionstring += "<option value=\"" + item.id + "\" >" + item.name + "</option>";
            });
            $("#departmentId").html('<option value=""></option>' + optionstring);
            _form.render('select'); //这个很重要
        });

    };
</script>

</body>
</html>